<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->headerdata = array();
		if (($this->session->userdata('adminid')>0) && ($this->session->userdata('adminhash')==md5($this->session->userdata('adminid').$this->session->userdata('adminemail')))){
			$this->headerdata['adminid'] = $this->session->userdata('adminid');
			$this->headerdata['adminemail'] = $this->session->userdata('adminemail');
			$this->headerdata['username'] = $this->session->userdata('adminemail');
		}else{
			redirect(base_url('/index.php/admin/login'));
		}
		
		$this->headerdata['route'] = 'AdminCP';
		$this->headerdata['routelink'] = 'admin';
		$this->headerdata['route2'] = 'Manage News';
		$this->headerdata['route2link'] = 'index.php/admin/news';
		
		if (($this->session->userdata('msg') != '') && ($this->session->userdata('msgtype') != '')){
			$this->headerdata['msg'] = $this->session->userdata('msg');
			$this->headerdata['msgtype'] = $this->session->userdata('msgtype');
			$this->session->set_userdata('msg','');
			$this->session->set_userdata('msgtype','');
		}
		
		$this->load->model('NewsModel');
	}
	
	function index(){
		$data = array();
		$q = $this->db->get('news');
		$data['list'] = $q->result();
		
		$this->load->view('admin/common/header',$this->headerdata);
		$this->load->view('admin/common/left');
		$this->load->view('admin/news',$data);
		$this->load->view('admin/common/footer');
	}
	
	function add(){
		$data = array();
		if ($this->input->post('title') != ''){
			$newdata = array(
				'title'			=> $this->input->post('title'),
				'content'		=> $this->input->post('content'),
				'companyid'		=> $this->input->post('companyid'),
				'time'			=> time()
			);
			$data = $newdata;
			
			if ($this->NewsModel->add($newdata)){
				$this->session->set_userdata('msg','News added.');
				$this->session->set_userdata('msgtype','success');
				redirect(base_url($this->headerdata['route2link']));
			}else{
				$this->headerdata['msgtype'] = 'error';
				$this->headerdata['msg'] = 'Error. Please try again.';
			}
		}
		$this->headerdata['route3'] = 'Add news';
		
		$this->load->model('CompanyModel');
		$data['companies'] = $this->CompanyModel->get();
		
		$this->load->library('ckeditor');
		$data['editor'] = new CKEditor(site_url().'js/ckeditor/');
		
		$this->load->view('admin/common/header',$this->headerdata);
		$this->load->view('admin/common/left');
		$this->load->view('admin/news_add',$data);
		$this->load->view('admin/common/footer');
	}
	
	function edit($id){
		if (!isset($id) || !is_numeric($id) || ($id < 1)) redirect(base_url($this->headerdata['route2link']));
		$news = $this->NewsModel->get($id);
		if (!$news) redirect(base_url($this->headerdata['route2link']));
		
		$data = array();
		if ($this->input->post('title') != ''){
			$newdata = array(
				'title'			=> $this->input->post('title'),
				'content'		=> $this->input->post('content'),
				'companyid'		=> $this->input->post('companyid'),
				'time'			=> time()
			);
			$data = $newdata;
			
			if ($this->NewsModel->edit($id,$newdata)){
				$this->session->set_userdata('msg','News edited.');
				$this->session->set_userdata('msgtype','success');
				redirect(base_url($this->headerdata['route2link']));
			}else{
				$this->headerdata['msgtype'] = 'error';
				$this->headerdata['msg'] = 'Error. Please try again.';
			}
		}
		$this->headerdata['route3'] = 'Edit news';
		
		$this->load->model('CompanyModel');
		$data['companies'] = $this->CompanyModel->get();
		$data['news'] = $news;
		
		$this->load->library('ckeditor');
		$data['editor'] = new CKEditor(site_url().'js/ckeditor/');
		
		$this->load->view('admin/common/header',$this->headerdata);
		$this->load->view('admin/common/left');
		$this->load->view('admin/news_edit',$data);
		$this->load->view('admin/common/footer');
	}
}
?>